/**
 * Created by lock
 * Date: 2019-08-09
 * Time: 18:18
 */
package connect

import (
	"fmt"
	"github.com/google/uuid"
	"go.uber.org/zap"
	"gochat/config"
	_ "net/http/pprof"
	"runtime"
	"time"
)

var DefaultServer *Server

type Connect struct {
	ServerId string
}

func New() *Connect {
	return new(Connect)
}

func (c *Connect) Run() {
	// 获取连接层配置
	connectConfig := config.Conf.Connect

	// 设置可以执行的最大CPU数
	runtime.GOMAXPROCS(connectConfig.ConnectBucket.CpuNum)

	// 初始化逻辑层rpc客户端，调用逻辑层rpc服务器
	if err := c.InitLogicRpcClient(); err != nil {
		zap.L().Panic("InitLogicRpcClient err:%s", zap.Error(err))
	}
	// 初始化连接层rpc服务器，逻辑客户端将调用此
	Buckets := make([]*Bucket, connectConfig.ConnectBucket.CpuNum)
	for i := 0; i < connectConfig.ConnectBucket.CpuNum; i++ {
		Buckets[i] = NewBucket(BucketOptions{
			ChannelSize:   connectConfig.ConnectBucket.Channel,
			RoomSize:      connectConfig.ConnectBucket.Room,
			RoutineAmount: connectConfig.ConnectBucket.RoutineAmount,
			RoutineSize:   connectConfig.ConnectBucket.RoutineSize,
		})
	}
	operator := new(DefaultOperator)
	DefaultServer = NewServer(Buckets, operator, ServerOptions{
		WriteWait:       10 * time.Second,
		PongWait:        60 * time.Second,
		PingPeriod:      54 * time.Second,
		MaxMessageSize:  512,
		ReadBufferSize:  1024,
		WriteBufferSize: 1024,
		BroadcastSize:   512,
	})

	c.ServerId = fmt.Sprintf("%s-%s","ws",uuid.New().String())
	// 初始化连接层rpc服务器，任务层将调用此
	if err := c.InitConnectWebsocketRpcServer(); err != nil {
		zap.L().Panic("InitConnectWebsocketRpcServer Fatal error: %s \n", zap.Error(err))
	}

	// 启动连接层服务器处理程序持久连接
	if err := c.InitWebsocket(); err != nil {
		zap.L().Panic("Connect layer InitWebsocket() error:  %s \n", zap.Error(err))
	}
}

func (c *Connect) RunTcp() {
	// 获取连接层配置
	connectConfig := config.Conf.Connect

	// 设置可以执行的最大CPU数
	runtime.GOMAXPROCS(connectConfig.ConnectBucket.CpuNum)

	// init逻辑层rpc客户端，调用逻辑层rpc服务
	if err := c.InitLogicRpcClient(); err != nil {
		zap.L().Panic("InitLogicRpcClient err:%s", zap.Error(err))
	}

	//init Connect layer rpc服务器，逻辑客户端将调用此
	Buckets := make([]*Bucket, connectConfig.ConnectBucket.CpuNum)
	for i := 0; i < connectConfig.ConnectBucket.CpuNum; i++ {
		Buckets[i] = NewBucket(BucketOptions{
			ChannelSize:   connectConfig.ConnectBucket.Channel,
			RoomSize:      connectConfig.ConnectBucket.Room,
			RoutineAmount: connectConfig.ConnectBucket.RoutineAmount,
			RoutineSize:   connectConfig.ConnectBucket.RoutineSize,
		})
	}

	operator := new(DefaultOperator)
	DefaultServer = NewServer(Buckets, operator, ServerOptions{
		WriteWait:       10 * time.Second,
		PongWait:        60 * time.Second,
		PingPeriod:      54 * time.Second,
		MaxMessageSize:  512,
		ReadBufferSize:  1024,
		WriteBufferSize: 1024,
		BroadcastSize:   512,
	})

	//go func() {
	//	http.ListenAndServe("0.0.0.0:9000", nil)
	//}()

	c.ServerId = fmt.Sprintf("%s-%s", "tcp", uuid.New().String())
	// 初始化连接层rpc服务器，任务层将调用此
	if err := c.InitConnectTcpRcpServer(); err != nil {
		zap.L().Panic("InitConnectWebsocketRpcServer Fatal error: %s \n", zap.Error(err))
	}
	// 通过tcp启动连接层服务器处理程序持久连接
	if err := c.InitTcpServer(); err != nil {
		zap.L().Panic("Connect layerInitTcpServer() error:%s\n ", zap.Error(err))
	}
}
